package simpleSort;

import java.util.Arrays;

/**
 * 无序数组中
 * 1.选择范围arr[i,length) 将最小值放在i位置上 ， 将最小元素和 i 位置上的元素进行互换
 * arr[0,i)排好序状态  arr[i,length)未排序
 * 2.i++
 * 3.i < length
 */
public class SelectSort {

    public static <E extends Comparable<E>> void sort(E[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minValueIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[minValueIndex].compareTo(arr[j]) > 0)
                    minValueIndex = j;
            }
            ArrTools.swap(arr, i, minValueIndex);
        }
    }

    public static <E extends Comparable<E>> void descSort(E[] arr) {
        for (int i = arr.length - 1; i >= 0; i--) {
            int maxValueIndex = i;
            for (int j = i - 1; j >= 0; j--) {
                if (arr[j].compareTo(arr[maxValueIndex]) > 0)
                    maxValueIndex = j;
            }
            ArrTools.swap(arr, i, maxValueIndex);
        }
    }


    public static void main(String[] args) {
        Integer[] arr = {5, 2, 6, 7, 8, 9, 12, 34, 86, -23, -12, -5};
        SelectSort.descSort(arr);
        String s = ArrTools.printArr(arr);
        System.out.println(s);

        int[] sizeArr = {10000, 10000};
        for (int i = 0; i < sizeArr.length; i++) {
            int n = sizeArr[i];
            System.out.println("random arr");
            Integer[] randomArray = ArrayGenerator.generatorRandomArray(n, n);
            Integer[] randomArray1 = Arrays.copyOf(randomArray, n);
            SortingHelper.sortTest("insertSort", randomArray);
            SortingHelper.sortTest("insertDescSort", randomArray1);

            System.out.println();
            System.out.println("order arr");
            Integer[] orderArray = ArrayGenerator.generatorOrderArray( n);
            Integer[] orderArray1 = Arrays.copyOf(orderArray, n);
            SortingHelper.sortTest("insertSort", randomArray);
            SortingHelper.sortTest("insertDescSort", randomArray1);

        }


//        long start = System.nanoTime();
//        sort(integers);
//        long end = System.nanoTime();
//        double time = (end-start)/1000000000.0;
//        if (!SortingHelper.isSorted(arr)){
//            throw new RuntimeException("select sort is failed");
//        }
//        System.out.println(time);

//        Student[] students = {new Student("abc",98),
//                            new Student("frank",100),
//                            new Student("bobo",99)};
//        sort(students);
//        for (int i = 0; i < students.length; i++) {
//            System.out.println(students[i]);
//        }


    }

    private SelectSort() {
    }
}
